package com.ruoyi.st.service.impl;

import com.ruoyi.st.domain.StTicketDate;
import com.ruoyi.st.domain.StTicketInfo;
import com.ruoyi.st.domain.StTicketPrice;
import com.ruoyi.st.mapper.StTicketDateMapper;
import com.ruoyi.st.mapper.StTicketInfoMapper;
import com.ruoyi.st.mapper.StTicketPriceMapper;
import com.ruoyi.st.service.IStTicketInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.StringUtils.substring;

/**
 * 售票信息 (st_ticket_info)Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-06
 */
@Slf4j
@Service
public class StTicketInfoServiceImpl implements IStTicketInfoService 
{
    @Autowired
    private StTicketInfoMapper stTicketInfoMapper;
    @Autowired
    private StTicketPriceMapper stTicketPriceMapper;
    @Autowired
    private StTicketDateMapper stTicketDateMapper;
    /**
     * 查询售票信息 (st_ticket_info)
     * 
     * @param id 售票信息 (st_ticket_info)主键
     * @return 售票信息 (st_ticket_info)
     */
    @Override
    public StTicketInfo selectStTicketInfoById(Long id)
    {
        return stTicketInfoMapper.selectStTicketInfoById(id);
    }

    @Override
    public StTicketInfo selectStTicketInfoByPerformanceId(Long performanceId) {
        log.debug("根据演出详情ID获取指定购票信息业务={}",performanceId);
        StTicketInfo stTicketInfo = stTicketInfoMapper.selectStTicketInfoByPerformanceId(performanceId);
        List<StTicketDate> stTicketDate = stTicketDateMapper.selectStTicketDateByTicketInfoId(stTicketInfo.getId());

        for (StTicketDate ticketDate : stTicketDate){

            List<StTicketPrice> stTicketPrices = stTicketPriceMapper.selectStTicketPriceByTicketDateId(ticketDate.getId());
            if(stTicketPrices != null && stTicketPrices.size()>0){
                ticketDate.setTicketList(stTicketPrices);
            }
            log.debug("day={}",ticketDate.getDay());
            ticketDate.setDay2(ticketDate.getDay());
            ticketDate.setWeekDay(str2(ticketDate.getDay().toString()));
            ticketDate.setTime(str3(ticketDate.getDay().toString()));
        }
        stTicketInfo.setList(stTicketDate);
        return stTicketInfo;
    }

    private static String str2(String originalString){

        if(originalString.contains("Mon")){
            return "周一";
        }else if(originalString.contains("Tue")){
            return "周二";
        }else if(originalString.contains("Wed")){
            return "周三";
        }else if(originalString.contains("Thu")){
            return "周四";
        }else if(originalString.contains("Fri")){
            return "周五";
        }else if(originalString.contains("Sat")){
            return "周六";
        }else if(originalString.contains("Sun")){
            return "周日";
        }
        return null;
    }

    private static String str3(String originalString){
        return originalString.substring(10,16);
    }

    /**
     * 查询售票信息 (st_ticket_info)列表
     * 
     * @param stTicketInfo 售票信息 (st_ticket_info)
     * @return 售票信息 (st_ticket_info)
     */
    @Override
    public List<StTicketInfo> selectStTicketInfoList(StTicketInfo stTicketInfo)
    {
        return stTicketInfoMapper.selectStTicketInfoList(stTicketInfo);
    }

    /**
     * 新增售票信息 (st_ticket_info)
     * 
     * @param stTicketInfo 售票信息 (st_ticket_info)
     * @return 结果
     */
    @Override
    public int insertStTicketInfo(StTicketInfo stTicketInfo)
    {
        return stTicketInfoMapper.insertStTicketInfo(stTicketInfo);
    }

    /**
     * 修改售票信息 (st_ticket_info)
     * 
     * @param stTicketInfo 售票信息 (st_ticket_info)
     * @return 结果
     */
    @Override
    public int updateStTicketInfo(StTicketInfo stTicketInfo)
    {
        return stTicketInfoMapper.updateStTicketInfo(stTicketInfo);
    }

    /**
     * 批量删除售票信息 (st_ticket_info)
     * 
     * @param ids 需要删除的售票信息 (st_ticket_info)主键
     * @return 结果
     */
    @Override
    public int deleteStTicketInfoByIds(Long[] ids)
    {
        return stTicketInfoMapper.deleteStTicketInfoByIds(ids);
    }

    /**
     * 删除售票信息 (st_ticket_info)信息
     * 
     * @param id 售票信息 (st_ticket_info)主键
     * @return 结果
     */
    @Override
    public int deleteStTicketInfoById(Long id)
    {
        return stTicketInfoMapper.deleteStTicketInfoById(id);
    }
}
